import { _decorator, Component, math, Node, NodeEventType, toDegree, Touch, Vec2 } from 'cc';
import { Player } from './Player';
const { ccclass, property } = _decorator;

const LOG_TITLE = 'Hocker:'

const Y_ONE = new Vec2(0, 1)
const offset = new Vec2()

@ccclass('Hocker')
export class Hocker extends Component {
    private _player: Player = null

    onLoad() {
        this._player = this.node.getComponent(Player)
    }   

    onEnable() {
        this.node.on(NodeEventType.TOUCH_MOVE, this.onTouchMoved, this)
    }

    onDisable() {
        this.node.off(NodeEventType.TOUCH_MOVE, this.onTouchMoved, this)
    }

    onTouchMoved(evt: Touch) {
        if (evt.getDelta().lengthSqr() <= 0)
            return

        Vec2.subtract(offset, evt.getLocation(), evt.getStartLocation()) 

        const cos = offset.normalize().dot(Y_ONE)
        let radius = Math.acos(cos)
        if (offset.x > 0) {
            radius = -radius
        }
        this._player.data_destAngle = Math.round((toDegree(radius) + 360) % 360)   //0~360
        // console.log(LOG_TITLE, 'destAngle:', this._player.data_destAngle, offset.lengthSqr())
    }
}


